//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import MebcardtypeZK from "../../views/mall/MebcardtypeZK.vue";
let extension = {
  components: {
    //动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: "", //{ template: "<div>扩展组xx件</div>" },
    gridBody: "",
    gridFooter: MebcardtypeZK,
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: "",
    modelBody: ""
    // modelFooter: () => import('../mall/product/application.vue')
  },
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //事件扩展
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    onInit () {
      // 不显示多选框
      // 显示序号
      // 开启单选
      let quota = this.getFormOption("quota");
      let basetype = this.getFormOption("basetype");
      let is_discount = this.getFormOption("is_discount");
      let supplierCode = this.getFormOption("supplierCode");
      let pointsCode = this.getFormOption("pointsCode");
      let isvalueCard = this.getFormOption("isvalueCard");
      let smsId = this.getFormOption("smsName");
      let signCode = this.getFormOption("signCode");
      let isMessage = this.getFormOption("isMessage");
      let spare10 = this.getFormOption("smsNote");
      // 短信绑定
      isMessage.onChange = (val, option) => {
        if (val == 1) {
          smsId.hidden = false;
          signCode.hidden = false;
          smsId.required = true;
          signCode.required = true;
        } else {
          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          smsId.hidden = true;
          signCode.hidden = true;
          spare10.hidden = true;
          smsId.required = false;
          signCode.required = false;
        }
      }
      smsId.onChange = (val, option) => {
        spare10.hidden = false;
        let data = option.data.filter(item => item.key == val);
        if (data.length > 0) { this.editFormFields[spare10.field] = data[0].remark; } else { spare10.hidden = true }
      }
      basetype.onChange = (v, i) => {
        if (v == 1) {
          is_discount.hidden = true;
          isvalueCard.hidden = false;
          quota.hidden = false;
          is_quota.hidden = false;
          this.editFormFields.is_discount = 0;
          this.editFormFields.is_quota = 1;
          this.editFormFields.isvalueCard = true;
        } else {
          is_discount.hidden = false;
          isvalueCard.hidden = true;
          quota.hidden = true;
          is_quota.hidden = true;
          this.editFormFields.is_discount = 1;
          this.editFormFields.is_quota = 0;
          this.editFormFields.isvalueCard = false;
        }
      };
      let is_quota = this.getFormOption("is_quota");
      is_quota.onChange = (v, i) => {
        if (v == 1) {
          quota.hidden = false;
        } else {
          quota.hidden = true;
        }
      };
      supplierCode.onChange = val => {
        pointsCode.hidden = false
        if (val.length) {
          this.http
            .get("/api/SupplierNews/GetPointss", { supplierCode: val.toString() })
            .then(res => {
              if (res.data && res.data.length != 0) {
                pointsCode.data = res.data.map(i => {
                  return {
                    key: i.pointsId,
                    value: i.pointsName
                  };
                });
              } else {
                pointsCode.data = [];
              }
            });
        }
      };
      this.columns.push({
        title: "操作",
        fixed: "right",
        width: 80,
        render: (h, { row, column, index }) => {
          return h(
            "a",
            {
              on: {
                click: () => {
                  this.$refs.gridFooter.model = true;
                  this.$refs.gridFooter.title = "新建";
                  this.$nextTick(() => {
                    this.$refs.gridFooter.$refs.myform.reset();
                    // this.$refs.gridFooter.cardMakeId = row.id
                    this.$refs.gridFooter.editFormFields.card_type =
                      row.cardtypename;
                    this.$refs.gridFooter.couponZK = row;
                    // this.$refs.gridFooter.editFormFields.cardType = row.cardtypename
                    // this.$refs.gridFooter.editFormFields.cardno_khgz = 0
                    // this.$refs.gridFooter.editFormFields.cardno_yxqgz = 0
                  });
                }
              }
            },
            "制卡"
          );
        }
      });
    },
    direct () {
      let isMessage = this.getFormOption("isMessage");
      let smsId = this.getFormOption("smsName");
      let signCode = this.getFormOption("signCode");//短信签名

      isMessage.hidden = false;
      if (this.editFormFields[isMessage.field] == 1) {
        smsId.required = true;
        signCode.required = true;
      }
      smsId.hidden = false;
      signCode.hidden = false;
    },
    rowClick ({ row, column, event }) {
      //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    async modelOpenBeforeAsync (row) {
      console.log(row,'弹窗打开前的操作当前行数据');
      let smsId = this.getFormOption("smsName");
      let signCode = this.getFormOption("signCode");
      let spare10 = this.getFormOption("smsNote");
      // 默认情况下 不必填 不展示
      smsId.hidden = true;
      signCode.hidden = true;
      spare10.hidden = true;
      smsId.required = false;
      signCode.required = false;
      let isEDIT = this.currentAction == this.const.EDIT;
      if (isEDIT) {
        // 编辑
        if (row.isMessage == 1) { //发送短信
          smsId.hidden = false;
          signCode.hidden = false;
          spare10.hidden = false;
          smsId.required = true;
          signCode.required = true;
        } else {
          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          smsId.hidden = true;
          spare10.hidden = true;
          signCode.hidden = true;
          smsId.required = false;
          signCode.required = false;
        }
      }
      return true;
    },
    modelOpenAfter (row) {
      let is_quota = this.getFormOption("is_quota");
      let smsId = this.getFormOption("smsName");
      if (this.currentAction == "Add") {
        this.editFormFields.is_discount = 0;
        this.editFormFields.is_realname = 0;
        this.editFormFields.basetype = "1";
        let is_discount = this.getFormOption("is_discount");
        let quota = this.getFormOption("quota");
        is_discount.hidden = true;
        is_quota.hidden = false;
        quota.hidden = false;
        this.editFormFields.is_quota = 1;
        this.editFormFields.isMessage = 0;
        this.editFormFields.IsBrushFace = 0;
      } else {
        this.editFormFields.pointsCode = []
        is_quota = row.is_quota == 1 || true ? true : false
        if (row.pointsCode != '' && row.pointsCode != null) {
          this.$nextTick(() => {
            let strArr = row.pointsCode.split(',')
            this.$set(this.editFormFields,"pointsCode",strArr.map(num => parseInt(num)))
          })
        } else {
          this.editFormFields.pointsCode = []
        }
        if (this.editFormFields.smsName) {
          this.editFormFields.smsNote = smsId.data.filter(_=> { return _.key == row.smsName })[0].remark;
        }
      }
    },
    addBefore (e) {
      e.mainData.is_quota = e.mainData.is_quota == 0 || false ? false : true;
      e.mainData.is_realname = e.mainData.is_realname == 0 ? false : true;
      e.mainData.is_discount = e.mainData.is_discount == 0 ? false : true;
      e.mainData.isvalueCard = e.mainData.isvalueCard ? true : false;
      e.detailData = [];
      delete e.mainData.smsNote;
      return true;
    },
    delBefore (ids, rows) {
      let p = {
        order: "desc",
        page: 1,
        rows: 10000,
        sort: "createtime",
        wheres: `[{\"name\":\"card_type\",\"value\":\"${rows[0].cardtypeid}\",\"displayType\":\"text\"}]`
      };
      this.http.post("/api/member_card_make/getPageData", p).then(res => {
        if (res.rows.length != 0) {
          this.$Message.error("此卡模板下有会员卡，暂不支持删除");
        } else {
          this.http.post("/api/Mebcardtype/del", [ids.toString()]).then(res => {
            this.refresh();
          });
        }
      });
    },
    updateBefore(e) {
      e.mainData.is_quota = e.mainData.is_quota == 0 || false ? false : true;
      e.mainData.is_realname = e.mainData.is_realname == 0 ? false : true;
      e.mainData.is_discount = e.mainData.is_discount == 0 ? false : true;
      e.mainData.isvalueCard = e.mainData.isvalueCard ? true : false;
      delete e.mainData.smsNote;
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true
    },
  }
};
export default extension;
